<?php
namespace app\command;


use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use app\common\service\GuguService;


class SchoolLineByprovince extends Command
{
    protected function configure()
    {
        $this->setName('school_line_byprovince')->setDescription('Your command description');
    }

    protected function execute(Input $input, Output $output)
    {
		// Db::name('school')->field('college_name') -> chunk(100, function($users) {
		   // foreach($users as $user)  {
				
			// }
		// });
		$year = 2022;
		$list = Db::name('area')->field('id,name')->where('pid',0)->order('id asc')->select();
		foreach($list as $key=>$province){
			$page = 1;
			$result = GuguService::make('/metadata/ceecollegeline',['appkey'=>'UZ8X3YJWLFJWJKY28U63K8YFUTQZX4RD','pageindex'=>$page,'pagesize'=>100,'searchtype'=>'PROVINCENAME','keyword'=>$province['name'],'year'=>$year,'sort'=>'LowestScore|desc']);
			if($result[0] != 200){
			   setLog('school_line_byprovince','接口出错1：'.$province['name']);
			   continue;
			}
			$page_total = ceil($result[1][0]['DataTotalCount']/100);
			if($page_total == 0){
				setLog('school_line_byprovince','拉取成功：'.$province['name'].' 没有数据');
			   continue;
			}
			
			$dataM = [];
			$nowtime = time();
			if(!empty($result[1][1])){
				foreach($result[1][1] as $k=>$v){
					$dataM[] = ['province_id'=>$province['id'],'province'=>$v['Province'],'school_uuid'=>$v['SchoolUUID'],'college_name'=>$v['CollegeName'],'year'=>$v['Year'],'highest_score'=>$v['HighestScore'],'average_score'=>$v['AverageScore'],'lowest_score'=>$v['LowestScore'],'lowest_rank'=>$v['LowestRank'],'provincial_controlLine'=>$v['ProvincialControlLine'],'enrollment_type'=>$v['EnrollmentType'],'selection_level'=>$v['SelectionLevel'],'admission_batch'=>$v['AdmissionBatch'],'type_name'=>$v['TypeName'],'course_selection'=>$v['CourseSelection'],
					'course_selection_name'=>$v['CourseSelectionName'],'subject_group'=>$v['SubjectGroup'],
					'school_type'=>$v['SchoolType'],'school_in_city'=>$v['SchoolInCity'],
					'is985'=>$v['Is985'],
					'is211'=>$v['Is211'],'is_dual_class'=>$v['IsDualClass'],'cover_image'=>$v['CoverImage'],
					'create_time'=>$nowtime,'update_time'=>$nowtime];
				}
			}
			if(!empty($dataM)){
				$flag = Db::name('school_line_new')->insertAll($dataM);
				if(!$flag){
					setLog('school_line_byprovince','插入出错：'.$province['name'].$page.'页');
				}
			}
			
			if($page_total == 1){
				
				setLog('school_line_byprovince','拉取成功：'.$province['name']);
				
			}else{
				for($page=2;$page<=$page_total;$page++){
					$result = GuguService::make('/metadata/ceecollegeline',['appkey'=>'UZ8X3YJWLFJWJKY28U63K8YFUTQZX4RD','pageindex'=>$page,'pagesize'=>100,'searchtype'=>'PROVINCENAME','keyword'=>$province['name'],'year'=>$year,'sort'=>'LowestScore|desc']);
					if($result[0] != 200){
					   setLog('school_line_byprovince','接口出错2：'.$province['name']);
					   continue;
					}
					$dataM = [];
					$nowtime = time();
					if(!empty($result[1][1])){
						foreach($result[1][1] as $k=>$v){
							$dataM[] = ['province_id'=>$province['id'],'province'=>$v['Province'],'school_uuid'=>$v['SchoolUUID'],'college_name'=>$v['CollegeName'],'year'=>$v['Year'],'highest_score'=>$v['HighestScore'],'average_score'=>$v['AverageScore'],'lowest_score'=>$v['LowestScore'],'lowest_rank'=>$v['LowestRank'],'provincial_controlLine'=>$v['ProvincialControlLine'],'enrollment_type'=>$v['EnrollmentType'],'selection_level'=>$v['SelectionLevel'],'admission_batch'=>$v['AdmissionBatch'],'type_name'=>$v['TypeName'],'course_selection'=>$v['CourseSelection'],
							'course_selection_name'=>$v['CourseSelectionName'],'subject_group'=>$v['SubjectGroup'],
							'school_type'=>$v['SchoolType'],'school_in_city'=>$v['SchoolInCity'],
							'is985'=>$v['Is985'],
							'is211'=>$v['Is211'],'is_dual_class'=>$v['IsDualClass'],'cover_image'=>$v['CoverImage'],
							'create_time'=>$nowtime,'update_time'=>$nowtime];
						}
					}
					if(!empty($dataM)){
						$flag = Db::name('school_line_new')->insertAll($dataM);
						if(!$flag){
							setLog('school_line_byprovince','插入出错：'.$province['name'].$page.'页');
						}
					}
				}
				setLog('school_line_byprovince','拉取成功：'.$province['name']);
			}
		}
		
		setLog('school_line_byprovince','执行完毕');exit();
		
    }


}
